#include "stdafx.h"
#include "TrainingCase.h"


CTrainingCase::CTrainingCase(void)
{
}


CTrainingCase::~CTrainingCase(void)
{
}

CTrainingCase::CTrainingCase( EOBJTYPE tEnvRoundMe[ ALL_DIRECTION_COUNT ] )
{
	memcpy( m_tEnvRoundMe, tEnvRoundMe, sizeof(EOBJTYPE)*ALL_DIRECTION_COUNT );
	memset( m_nInputSignalSequence, 0, sizeof(m_nInputSignalSequence) );
	memset( m_nCurrentSignal, 0, sizeof(m_nCurrentSignal) );

	for( int j=0; j<ALL_DIRECTION_COUNT; j++ )
	{
		if( m_tEnvRoundMe[j] == EOBJTYPE_WATER )
		{
			m_nInputSignalSequence[j][0] = SIGNAL_BASE;
			for( int k=1; k<SIGNAL_PERIOD; k++ )
				m_nInputSignalSequence[j][k] = SIGNAL_ZERO;
		}
		else if ( m_tEnvRoundMe[j] == EOBJTYPE_FOOD )
		{
			for( int k=0; k<SIGNAL_PERIOD; k++ )
			{
				if( k == 0 || k==SIGNAL_PERIOD/2 )
					m_nInputSignalSequence[j][k] = SIGNAL_BASE;
				else
					m_nInputSignalSequence[j][k] = SIGNAL_ZERO;
			}
		}
		else if ( m_tEnvRoundMe[j] == EOBJTYPE_CREATURE )
		{
			m_nInputSignalSequence[j][0] = -SIGNAL_BASE;
			for( int k=1; k<SIGNAL_PERIOD; k++ )
				m_nInputSignalSequence[j][k] = 0;
		}
	}
}

int CTrainingCase::GetSignalForNervus( int nNervusIndex )
{
	if( m_nCurrentSignal[nNervusIndex] < SIGNAL_PERIOD )
		return m_nInputSignalSequence[nNervusIndex][m_nCurrentSignal[nNervusIndex]++];
	else
		return SIGNAL_ZERO;
}

RESULT_JUDGMENT CTrainingCase::CompareNervusOutputSignal( void )
{
	DIRECTION tDir;
	int nCount=0;

	for( int i=0; i<OUTPUT_SIGNAL_COUNT; i++ )
	{
		//TRACE( "Out-->[%d: %d]\n", i, m_nOutputSignal[i]);
		if( m_nOutputSignal[i]>=SIGNAL_BASE + MAX_RUN_STEP/SIGNAL_PERIOD*SIGNAL_BASE )
		{
			tDir = (DIRECTION)i;
			nCount++;
		}
	}
	if( nCount!=1 )
		return RES_COMMON;

	if( tDir == DIR_NONE )
	{
		int i;
		for( i=0; i<ALL_DIRECTION_COUNT; i++ )
			if( m_tEnvRoundMe[i] == EOBJTYPE_FOOD )
				return RES_COMMON;
		for( i=0; i<ALL_DIRECTION_COUNT; i++ )
			if( m_tEnvRoundMe[i] != EOBJTYPE_CREATURE )
				break;

		if( i == ALL_DIRECTION_COUNT )
			return RES_GOOD;
		else
			return RES_COMMON;
	}

	if( m_tEnvRoundMe[ tDir ] == EOBJTYPE_CREATURE )
		return RES_BAD;
	if( m_tEnvRoundMe[ tDir ] == EOBJTYPE_FOOD )
		return RES_GOOD;
	if( m_tEnvRoundMe[ tDir ] == EOBJTYPE_WATER )
	{
		int i;
		for( i=0; i<ALL_DIRECTION_COUNT; i++ )
			if( m_tEnvRoundMe[i] == EOBJTYPE_FOOD )
				return RES_COMMON;
		if( i==ALL_DIRECTION_COUNT )
			return RES_GOOD;
		return RES_COMMON;
	}
	return RES_BAD;
}

